package com.ruyuan.starter.async.handler;

import com.lmax.disruptor.WorkHandler;
import com.ruyuan.starter.async.event.WorkEvent;
import com.ruyuan.starter.async.listener.EventListener;

/**
 * @author zhonghuashishan
 */
public class WorkEventHandler implements WorkHandler<WorkEvent> {

    @Override
    public void onEvent(WorkEvent event) throws Exception {
        try {
            processWorkEvent(event);
        } finally {
            event.clear();
        }
    }

    /**
     * 这里才开始处理事件
     */
    @SuppressWarnings("unchecked")
    private void processWorkEvent(WorkEvent event) {
        for (EventListener listener : event.listeners) {
            listener.onEvent(event.event, event.context);
        }
    }
}